package com.chatdating.mapper;


import com.chatdating.entity.Session;
import com.chatdating.entity.User;
import com.chatdating.entity.dto.ContactDto;
import com.chatdating.entity.vo.UserByNameVo;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM user WHERE username = #{username} and password = #{password} ")
    User findByNameAndPassword(@Param("username") String username, @Param("password") String password);

    //根据用户 id 查询用户
    @Select("select * from user where id=#{userId}")
    User findById(Integer userId);

    //根据用户名称查询用户
    @Select("SELECT * FROM user WHERE username = #{username}")
    User findByName(String name);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer insert(User user);

    @Update("UPDATE user SET authstatus = 2 where username = #{username}")
    void passAuthentication(String username);
    @Update("UPDATE user SET authstatus = 1 where username = #{username}")
    void failAuthentication(String username);

    List<User> selectUsersByUsername(@Param("username") String username);

    @Select("SELECT username FROM user WHERE id = #{authorId}")
    String getNameById(Integer authorId);

    //根据用户id 查询会话信息
    List<ContactDto> querySessionByName(String name);

    //根据sessionid 更新最后一条消息
    void  updateSession(Session session);

    //根据发送者和sessionid 查出接收人id
    int queryContactBySessionAndUserId(@Param("Sessionid") String Sessionid,@Param("username") String username);

    void updateLoginTimeByUsername(@Param("loginTime")String loginTime,@Param("username")String userName);

    /**
     *
     * @param lastOfflineTime  最后登录时间
     * @param contactId        联系人 id
     * @return
     */
    @MapKey("sessionId")
    Map<String, Integer> countUnreadMessagesBySession(
            @Param("lastOfflineTime") Long lastOfflineTime,
            @Param("contactId") String contactId
    );
    @Select("SELECT * FROM user WHERE username =  #{username}")
    UserByNameVo getUserInfo(@Param("username")String username);
    @Update("UPDATE user SET age = #{age}, gender = #{gender}, address = #{address}, head = #{head}, introduce = #{introduce}, phone = #{phone}, school = #{school}, qualification = #{qualification}, work = #{work}, presentAddress = #{presentAddress}, photos = #{photos}, authstatus = #{authstatus} WHERE username = #{username}")
    void updateUser(UserByNameVo user);
    @Select("SELECT id FROM user WHERE username = #{username}")
    String getAuthorIdByUsername(String username);
    @Update("UPDATE user set deleted = 1,username = '账号注销' where username = #{username}")
    void deleteUser(String username);


    void updateLastTime (@Param("lastTime")String lastTime,@Param("username")String userName);
}
